#include "ChatServer.h"

map<string,string> passwd={{"root","123456"}};
User root,zhong;
map<string,User> alluser={{"root",root},{"zhong",zhong}};  //用户名类和字符串映射
Post firstpost("root","1-1-1-1","default","first post");
vector<Post> postmsg={firstpost};
int sockfd=0,t=0;

void *deal_client(void *connfd)
{
char tmp[10];
string user,command;
string num,sbuff;
user=DealLogin(*(int *)connfd);
while(1)
{
/*if(dealdata(*(int *)connfd)=="number")
{
num=alluser[user].getmsgnum()
write(connfd,num.c_str(),sizeof(num.c_str()));
}*/
command=dealdata(*(int *)connfd);
if(command=="chat")
{
sbuff="chat";       //接到消息，请发下一个
write(*(int *)connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
dealchat(*(int *)connfd,user);
}
else if(command=="post")
{
cout<<"ffff"<<endl;
sbuff="post";       //接到消息，请发下一个
write(*(int *)connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
dealpost(*(int *)connfd,user);
}/*
else if(command=="message")
{sbuff="message";       //接到消息，请发下一个
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
dealmsg(*(int *)connfd);
}*/
else if(command=="close")
{
sbuff="close";       //接到消息，请发下一个
write(*(int *)connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
close(*(int*)connfd);
cout<<"client exit"<<endl;
break;
}
else     //异常断开连接，套接字收到未定义内容
{close(*(int*)connfd);
cout<<"abonomal disconnection"<<endl;
break;}}
t--;
}

int main()
{
struct sockaddr_in servaddr;
int connfd;
pthread_t ntid[MAX_CLIENT];
ConnectClient(servaddr);
while(1){
connfd=accept(sockfd,NULL,NULL);
if(connfd<0)
{
cout<<"server accept error"<<endl;
exit(0);
}
if(t<MAX_CLIENT)
{pthread_create(&ntid[t],NULL,deal_client,(void *)&connfd);
cout<<"creat successful"<<endl;
t++;}
else cout<<"client is full"<<endl;
cout<<"num of user: "<<t<<endl;
}
}
